var Hawkeye = {
	time: (new Date()).getTime() + '-',
	i: 0,
	unique: function () {
		return this.time + (this.i++);
	},
	getCookie: function (name) {
		var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : null;
	},
	setCookie: function (c_name, value, exdate) {
		if (exdate == '') {
			document.cookie = c_name + "=" + escape(value);
		} else {
			document.cookie = c_name + "=" + escape(value) + ";expires=" + exdate.toGMTString();
		}
	},
	guid: function () {
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
			var r = Math.random() * 16 | 0,
				v = c == 'x' ? r : (r & 0x3 | 0x8);
			return v.toString(16);
		});
	},
	setUidCookie: function (params) {
		hawkeye_baseurl = baseDomain + '/wj.hawkeye.gif?'
		uuidName = 'hawkeye_id';
		if (!this.getCookie(uuidName)) {
			var exdate = new Date();
			exdate.setYear(2050);
			this.setCookie(uuidName, this.guid(), exdate = exdate);
		}
	},
	genUrlStrFromDict: function (infodict) {
		// 根据传入的字典 拼接字符串 作为GET请求的URL的参数
		var s = ''
		for (var key in infodict) {
			if (!infodict[key]) {
				continue;
			}
			keyvaluestring = key + '=' + infodict[key]
			if (s == '') {
				s = s + keyvaluestring;
			} else {
				s = s + '&' + keyvaluestring;
			}
		}
		return s
	},
	imgLog: function (imgurl) {
		// 传入参数，以图片形式附带参数请求 来记录log
		var data = window['imgLogData'] || (window['imgLogData'] = {});
		var img = new Image();
		var uid = this.unique();
		img.onload = img.onerror = function () { //销毁一些对象  
			img.onload = img.onerror = null;
			img = null;
			delete data[uid];
		}
		// infodict['_uid'] = uid;
		// urlTailFromDict = genUrlStrFromDict(infodict);
		// img.src = hawkeye_baseurl + urlTailFromDict;

		imgurl = imgurl + '&_uid=' + uid;
		img.src = imgurl;
	},
	getCookiesInfo: function () {
		names = ['sessionid', 'hawkeye_mid', 'hawkeye_id'];
		var info = {};
		for (var i = names.length - 1; i >= 0; i--) {
			value = this.getCookie(names[i]);
			info[names[i]] = value;
		}
		return info
	},
	updateTwoDict: function (dict1, dict2) {
		for (var key in dict2) {
			dict1[key] = dict2[key];
		}
		return dict1
	},
	logPageUrl: function () {
		// 记录页面加载事件

		// 先检查记录 触发进入当前页面的上一个页面的按钮事件
		if (localStorage.buttoneventurl) {
			this.imgLog(localStorage.buttoneventurl);
			localStorage.buttoneventurl = '';
		}


		infodict = {
			'pageurl': encodeURIComponent(window.location.href),
			'eventtype': 'loadhtml',
		}
		infodict = this.updateTwoDict(infodict, this.getCookiesInfo());
		urlTailFromDict = this.genUrlStrFromDict(infodict);
		imgurl = hawkeye_baseurl + urlTailFromDict;
		this.imgLog(imgurl);
	},
	hawkeyeButtonClickHandler: function (buttonname) {
		// 处理监控到的按钮点击事件
		let infodict = {
			'pageurl': encodeURIComponent(window.location.href),
			'eventtype': 'buttonclick',
			'buttonname': encodeURIComponent(buttonname),
		}
		infodict = this.updateTwoDict(infodict, this.getCookiesInfo());
		urlTailFromDict = this.genUrlStrFromDict(infodict);
		buttoneventurl = hawkeye_baseurl + urlTailFromDict;
		localStorage.buttoneventurl = buttoneventurl;
	},
	init: function () {
		this.setUidCookie();
		this.logPageUrl();
	}
};

Hawkeye.init();
